﻿using UnityEngine;
using System.Collections;
using UnityEngine.Experimental.Networking;
using System;
namespace yoyo
{

    public class WWWTools : MonoBehaviour
    {
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="path">地址</param>
        /// <param name="json">参数</param>
        /// <param name="act">回调</param>
        public void WWWPost(string path, WWWForm json, Action<UnityWebRequest> act)
        {
            StartCoroutine(PostDown(path, json, act));
        }
        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="path">地址</param>
        /// <param name="act">回调</param>
        public void WWWGet(string path, Action<UnityWebRequest> act)
        {
            StartCoroutine(GetDown(path, act));
        }
        /// <summary>
        /// AB资源请求
        /// </summary>
        /// <param name="path">地址</param>
        /// <param name="act">回调</param>
        public void WWWAssetBundle(string path ,Action<AssetBundle> act)
        {
            StartCoroutine(DownBundel(path, act));
        }
        /// <summary>
        /// Texture2D资源请求
        /// </summary>
        /// <param name="path"></param>
        /// <param name="act"></param>
        public void WWWGetTexuture2D(string path ,Action<Texture2D> act)
        {
            StartCoroutine(DownTexture2D(path, act));
        }




        IEnumerator PostDown(string path, WWWForm json, Action<UnityWebRequest> act)
        {
            using (UnityWebRequest www = UnityWebRequest.Post(path, json))
            {
                yield return www.Send();
                if (www.isDone)
                {
                    act(www);
                }
            }
        }
        IEnumerator GetDown(string path, Action<UnityWebRequest> act)
        {
            using (UnityWebRequest www = UnityWebRequest.Get(path))
            {
                yield return www.Send();
                if (www.isDone)
                {
                    act(www);
                }
            }
        }
        IEnumerator DownBundel(string path ,Action<AssetBundle> act)
        {
            using (UnityWebRequest www = UnityWebRequest.GetAssetBundle(path))
            {
                yield return www.Send();
                DownloadHandlerAssetBundle down = (DownloadHandlerAssetBundle)www.downloadHandler;
                AssetBundle bundle = down.assetBundle;
                if(bundle)
                {
                    Debug.Log("下载成功");
                    act(bundle);
                }
                else
                {
                    Debug.Log("下载失败");
                }
            }
        }
        IEnumerator DownTexture2D(string path ,Action<Texture2D> act)
        {
            using (UnityWebRequest www = UnityWebRequest.GetTexture(path))
            {
                yield return www;
                if(www.isDone)
                {
                    DownloadHandlerTexture dht=(DownloadHandlerTexture)www.downloadHandler;
                    act(dht.texture);
                }
            }

        }
    }

}